#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <signal.h>
#include "proto.h"

int msgid;

void cleanFunction()
{
	msgctl(msgid, IPC_RMID, NULL);
	printf("clean over!\n");
}

void handler(int s)
{
	atexit(cleanFunction);
	exit(0);
}

int main()
{
	struct studentMsg rcvbuf;
	key_t key;

	key = ftok(FTOKNAME, PROJID);
	if (key < 0)
	{
		perror("ftok()");
		exit(1);
	}

	msgid = msgget(key, IPC_CREAT | 0666);
	if (msgid < 0)
	{
		perror("msgget()");
		exit(1);
	}

	signal(SIGINT, handler);
	while (1)
	{
		if (msgrcv(msgid, &rcvbuf, sizeof(rcvbuf) - sizeof(long), 0, 0) < 0)
		{
			perror("msgrcv()");
			exit(1);
		}
		printf("%s\n",rcvbuf.name);
		printf("%d\n",rcvbuf.age);
		printf("%d\n",rcvbuf.math);
		printf("%d\n",rcvbuf.English);
	}

}










